package eu.news.servlet.query;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import eu.jk.web.template.ErrorInstruction;
import eu.news.data.hibernate.dao.HB_NewsDAO;
import eu.news.data.hibernate.pojo.HB_News;
import eu.news.index.NewsIndexDAO;
import eu.news.template.WebPageHelper;

/**
 * Servlet implementation class Search
 */
public class Search extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Search() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String keyword=request.getParameter("keyword");
		HB_NewsDAO dao=new HB_NewsDAO();
		List<HB_News> newses=new ArrayList<HB_News>();
		for(int id:NewsIndexDAO.search(keyword)){
			HB_News news=dao.getById(id);
			if(news!=null)
				newses.add(news);
		}
		try {
			response.setCharacterEncoding("UTF-8");
			response.getWriter().print(WebPageHelper.searchResult(newses, keyword, request));
		} catch (ErrorInstruction e) {
			e.printStackTrace();
		}
	}

}
